//
//  AXFRightTableViewCell.m
//  闪电超市
//
//  Created by 李典 on 16/11/4.
//  Copyright © 2016年 李典. All rights reserved.
//

#import "AXFRightTableViewCell.h"
#import "AXFDetailsModel.h"
#import "UIImageView+WebCache.h"
#import "AXFAddGoodsView.h"

@interface AXFRightTableViewCell () <AXFAddGoodsView>
// 图片
@property (nonatomic, weak) UIImageView *icon;
// 产品名称
@property (nonatomic, weak) UILabel *name;
// 重量
@property (nonatomic, weak) UILabel *specifics;
// 价格
@property (nonatomic, weak) UILabel *min_price;
//精选图片
@property (nonatomic, weak) UIImageView *jingXuanImage;
// 原价
@property (nonatomic, weak) NSString *market_price;
//   原价画线
@property (nonatomic, strong)NSMutableAttributedString *str;
// 原价拉lable 记录
@property (nonatomic, weak) UILabel *originalLable;
@property (nonatomic, strong) AXFFoodModel *foodModel;

// 防止商品数量复用 在model里面放一份
@property (nonatomic, weak) AXFAddGoodsView *shopCountView;



@end

@implementation AXFRightTableViewCell



#pragma mark - 实现加号按钮的代理方法
- (void)addGoodsViewWith:(AXFAddGoodsView *)addGoodsView buyCount:(NSInteger)shopCount
{
    _model.shopCount = shopCount;
    //获取主窗口    需要计算  点击的按钮在View上的坐标  所以  直接在主窗口上获取比较方便
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    //定义一个属性   cell可以访问的到   然后获取点击按钮的位置
    CGPoint picturePoint = [self convertPoint:self.icon.center toView:keyWindow];
    //    CGPoint picturePoint = self.icon.center;
    if (addGoodsView.isPlus) {
        if ([self.delegate respondsToSelector:@selector(addGoodsWithCell:withAddModel:andPicturePoint:)]) {
            [self.delegate addGoodsWithCell:self withAddModel:_model andPicturePoint:picturePoint];
        }
    }
    
    //    if (addGoodsView.isPlus) {
    //        if ([self.description respondsToSelector:@selector(addGoodsWithCell:withAddModel:model:andPicturePoint:)]) {
    //            [self.delegate addGoodsWithCell:self withAddModel:_model model:_foodModel andPicturePoint:picturePoint];
    //        }
    //    }
    else{
        if ([self.delegate respondsToSelector:@selector(minusGoodsWithCell:withMinusModel:)]) {
            [self.delegate minusGoodsWithCell:self withMinusModel:_model];
            
        }
    }
}


- (void)setModel:(AXFDetailsModel *)model
{
    _model = model;
    
    // url 路径
    NSString *urlStr = model.img;
    NSURL *url = [NSURL URLWithString:urlStr];
    //    [_icon sd_setImageWithURL:url];
    [_icon sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"icon_icons_holder"]];
    _icon.contentMode =  UIViewContentModeScaleAspectFit;
    
    self.market_price = model.market_price;
    self.name.text = model.name;
    self.min_price.text = [NSString stringWithFormat:@"¥ %.2lf",model.partner_price];
    self.specifics.text = model.specifics;
    //  为了防止cell加减的数据复用就在 model中保存一份数据
    _shopCountView.shopCount = model.shopCount;
    
    
    if (model.is_xf) {
        self.jingXuanImage.image = [UIImage imageNamed:@"jingxuan.png"];
    }
    else
    {
        self.jingXuanImage.image = [UIImage imageNamed:@""];
    }
    NSString *marketPrice = [NSString stringWithFormat:@"¥%@",_model.market_price];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:marketPrice];
    //    self.str = str;
    [str setAttributes:@{NSStrikethroughStyleAttributeName:@(1),NSStrikethroughColorAttributeName:[UIColor grayColor]} range:NSMakeRange(0, str.length)];
    //    [str setAttributes:@{NSBaselineOffsetAttributeName:@(0)} range:NSMakeRange(0, 0)];
    self.originalLable.attributedText = str.copy;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addGoodsViewWith:buyCount:) name:@"homeToMarket" object:nil];
    }
    return self;
}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"homeToMarket" object:nil];
}

- (void)setupUI
{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //MARK: 创建控件
    // 图片
    UIImageView *imageIcon = [[UIImageView alloc]init];
    [self.contentView addSubview:imageIcon];
    
    //精选
    UIImageView *jingXuanImage = [[UIImageView alloc]init];
    //    jingXuanImage.backgroundColor = [UIColor blueColor];
    //    jingXuanImage.image = [UIImage imageNamed:@"da_mu_ge"];
    //    jingXuanImage.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:jingXuanImage];
    
    
    // 商品名称
    UILabel *nameLable = [[UILabel alloc]init];
    [self.contentView addSubview:nameLable];
    nameLable.font = [UIFont systemFontOfSize:14];
    
    // 商品重量
    UILabel *weightLable = [[UILabel alloc]init];
    [self.contentView addSubview:weightLable];
    weightLable.font = [UIFont systemFontOfSize:12];
    
    
    // 商品原价
    UILabel *originalLable = [[UILabel alloc]init];
    [self.contentView addSubview:originalLable];
    originalLable.textColor = [UIColor grayColor];
    originalLable.font = [UIFont systemFontOfSize:12];
    
    // 商品价格
    UILabel *pricrLable = [[UILabel alloc]init];
    [self.contentView addSubview:pricrLable];
    pricrLable.textColor = [UIColor redColor];
    pricrLable.font = [UIFont systemFontOfSize:14];
    
    // 商品加减
    AXFAddGoodsView *addView = [AXFAddGoodsView addGoodsView];
    //    AXFAddGoodsView *addView = [[AXFAddGoodsView alloc]init];
    [self.contentView addSubview:addView];
    //    [addView sizeToFit];
    
    addView.model = self.model;
    addView.delegate = self;
    
    
    //关联
    self.icon = imageIcon;
    self.name = nameLable;
    self.specifics = weightLable;
    self.min_price = pricrLable;
    self.originalLable = originalLable;
    self.jingXuanImage = jingXuanImage;
    self.shopCountView = addView;
    
    //MARK: MAS布局
    //    imageIcon.backgroundColor = [UIColor redColor];
    [imageIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.left.equalTo(self.contentView).offset(0);
        make.height.mas_equalTo(75);
        make.width.mas_equalTo(75);
    }];
    
    //    nameLable.text = @"可口可乐";
    //    nameLable.backgroundColor = [UIColor redColor];
    [nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageIcon).offset(5);
        make.left.equalTo(imageIcon.mas_right).offset(5);
        make.width.lessThanOrEqualTo(@160);
    }];
    
    //    jingXuanImage.backgroundColor = [UIColor yellowColor];
    [jingXuanImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLable);
        make.left.equalTo(nameLable.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(28, 14));
    }];
    
    //    weightLable.text = @"123123g";
    [weightLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLable.mas_bottom).offset(10);
        make.left.equalTo(nameLable);
    }];
    
    //    pricrLable.text = @"¥ 98";
    [pricrLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLable);
        make.top.equalTo(weightLable.mas_bottom).offset(10);
    }];
    
    [addView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(pricrLable.mas_bottom);
        make.right.equalTo(self.contentView);
        make.size.mas_equalTo(addView.bounds.size);
    }];
    
    [originalLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pricrLable.mas_right).offset(10);
        make.top.equalTo(pricrLable);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
